package com.fwtai.service.api;

import com.fwtai.api.ApiFeedbackDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 反馈建议业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-09-06 11:49:23
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ApiFeedbackService{

    @Resource
    private ApiFeedbackDao apiFeedbackDao;

    public String add(final HttpServletRequest request){
        final PageFormData formData = new PageFormData(request);
        final String p_details = "details";
        final String validate = ToolClient.validateField(formData,p_details);
        if(validate != null)return validate;
        if(formData.getString(p_details).length() > 2048){
            return ToolClient.createJsonFail("反馈内容内容字数太长");
        }
        formData.put("kid",new IdBuilder().getId121L());
        formData.put("loginId",LocalUserId.get());
        return ToolClient.executeRows(apiFeedbackDao.add(formData));
    }

    public String queryById(final String kid){
        if(kid == null || kid.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        return ToolClient.queryJson(apiFeedbackDao.queryById(kid));
    }

    public String delById(final String kid){
        if(kid == null || kid.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        final String exist_key = apiFeedbackDao.queryExistById(kid);
        if(exist_key == null){
            return ToolClient.createJsonFail("删除失败,数据已不存在");
        }
        final PageFormData formData = new PageFormData();
        formData.put("kid",kid);
        formData.put("loginId",LocalUserId.get());
        return ToolClient.executeRows(apiFeedbackDao.delById(formData));
    }

    //适用于api接口
    public String listDataPage(final HttpServletRequest request){
        final PageFormData formData = ToolClient.pageParamsApi(request);
        if(formData == null) return ToolClient.jsonValidateField();
        formData.put("loginId",LocalUserId.get());
        return ToolClient.jsonPage(apiFeedbackDao.listData(formData));
    }
}